<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use yii\web\UploadedFile;


class UploadedFileController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [ 
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
    /**
     * [actionUpload description]
     * @return [type] [description]
     */
	public function actionUpload()
	{	
		if(empty($_FILES))
			$json = ['state'=>'0','img'=>''];
		$file = array_keys($_FILES);
		$files = UploadedFile::getInstancesByName(reset($file));
        // var_dump(Yii::getAlias("@webroot").'data');die();
		foreach ($files as $filenum => $fileval) {
			$dir = '/data/'.date('Y').'/'.date('m').'/'.date('d');
		    $path = $dir.'/'.uniqid().'_'.$fileval->name;
		    if(!is_dir($dir)){
		    	@mkdir(Yii::getAlias("@webroot").$dir,0777,true);
		    }
            @move_uploaded_file($fileval->tempName,Yii::getAlias("@webroot").'/'.$path);
            $json = ['state'=>'1','img'=>$path];
		}
		die(json_encode($json));
	}
}